package com.jdz.gxq.info.admin.loan.service;

import java.util.List;
import java.util.Map;

import com.jdz.gxq.info.admin.loan.bo.Loan;

import jp.terasoluna.fw.dao.QueryDAO;
import jp.terasoluna.fw.dao.UpdateDAO;

public class LoanServiceImpl implements LoanService {
	
	private QueryDAO queryDAO = null;
	
	private UpdateDAO updateDAO = null;
	
	/**
	 * Loan检索分页用，查询记录条数
	 * @return
	 */
	public int getLoansCount(String enterpriseName) {
		
		int totalCount = queryDAO.executeForObject("loan.getLoansCount",
				enterpriseName, Integer.class);
		
		return totalCount;
	}
	
	/**
	 * Loan检索
	 * @return
	 */
	public List<Loan> getLoans(Map<String, Object> params) {
		
		return queryDAO.executeForObjectList("loan.getLoans", params);
	}
	
	/**
	 * 追加Loan
	 * @param loan
	 */
	public void insertLoan(Loan loan) {
		
		updateDAO.execute("loan.insertLoan", loan);
	}
	
	/**
	 * 更新Loan
	 * @param loan
	 */
	public void updateLoan(Loan loan) {
		
		updateDAO.execute("loan.updateLoan", loan);
	}
	
	/**
	 * 通过id得到loan
	 * @param id
	 * @return
	 */
	public Loan getLoanById(int enterpriseId) {

		Loan loan = queryDAO.executeForObject("loan.getLoanById",
				enterpriseId, Loan.class);
		
		return loan;
	}
	
	/**
	 * 删除Loan
	 * @param loan
	 */
	public void deleteLoan(int enterpriseId) {
		
		updateDAO.execute("loan.deleteLoanById", enterpriseId);
	}
	
	/**
	 * 验证loanName是否重复
	 * flag: 1 追加时验证
	 * flag: 2 更新时验证
	 */
	public boolean checkLoanName(Loan paramLoan, int flag) {
		
		Loan loan = queryDAO.executeForObject("loan.getLoanByName",
				paramLoan.getEnterpriseName(), Loan.class);

		if (loan == null) {
			return true;
		}
		
		// 更新验证，查出来的数据是不是本身
		if (flag == 2) {
			
			if (loan.getId() == paramLoan.getId()) {
				return true;
			}
		}
		
		return false;
	}

	public void setQueryDAO(QueryDAO queryDAO) {
		this.queryDAO = queryDAO;
	}

	public void setUpdateDAO(UpdateDAO updateDAO) {
		this.updateDAO = updateDAO;
	}
	
	
}
